﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using BMSQ.DBUtility;//Please add references
namespace BMSQ.DAL
{
	/// <summary>
	/// 数据访问类:Vendor_List
	/// </summary>
	public partial class Vendor_List
	{
		public Vendor_List()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string Vendor_ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Vendor_List");
			strSql.Append(" where Vendor_ID=@Vendor_ID ");
			SqlParameter[] parameters = {
					new SqlParameter("@Vendor_ID", SqlDbType.NVarChar,50)			};
			parameters[0].Value = Vendor_ID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(BMSQ.Model.Vendor_List model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Vendor_List(");
			strSql.Append("Vendor_ID,User_ID,Vendor_Name,Applyer,Address,Vendor_Image,Vendor_Phone,Vendor_Description,B_Type,longitude,Latitude,Delivery_MinimalMoney,Delivery_AdditionalMoney,Delivery_AlwaysNeedMoney,Delivery_Description,Delivery_Area,Vendor_StartTime,Vendor_EndTime,Record_Date)");
			strSql.Append(" values (");
			strSql.Append("@Vendor_ID,@User_ID,@Vendor_Name,@Applyer,@Address,@Vendor_Image,@Vendor_Phone,@Vendor_Description,@B_Type,@longitude,@Latitude,@Delivery_MinimalMoney,@Delivery_AdditionalMoney,@Delivery_AlwaysNeedMoney,@Delivery_Description,@Delivery_Area,@Vendor_StartTime,@Vendor_EndTime,@Record_Date)");
			SqlParameter[] parameters = {
					new SqlParameter("@Vendor_ID", SqlDbType.NVarChar,50),
					new SqlParameter("@User_ID", SqlDbType.NVarChar,50),
					new SqlParameter("@Vendor_Name", SqlDbType.NVarChar,50),
					new SqlParameter("@Applyer", SqlDbType.NVarChar,50),
					new SqlParameter("@Address", SqlDbType.NVarChar,200),
					new SqlParameter("@Vendor_Image", SqlDbType.NVarChar,200),
					new SqlParameter("@Vendor_Phone", SqlDbType.NVarChar,50),
					new SqlParameter("@Vendor_Description", SqlDbType.Text),
					new SqlParameter("@B_Type", SqlDbType.NVarChar,50),
					new SqlParameter("@longitude", SqlDbType.Decimal,9),
					new SqlParameter("@Latitude", SqlDbType.Decimal,9),
					new SqlParameter("@Delivery_MinimalMoney", SqlDbType.Int,4),
					new SqlParameter("@Delivery_AdditionalMoney", SqlDbType.Int,4),
					new SqlParameter("@Delivery_AlwaysNeedMoney", SqlDbType.Int,4),
					new SqlParameter("@Delivery_Description", SqlDbType.NVarChar,500),
					new SqlParameter("@Delivery_Area", SqlDbType.NVarChar,500),
					new SqlParameter("@Vendor_StartTime", SqlDbType.NVarChar,5),
					new SqlParameter("@Vendor_EndTime", SqlDbType.NVarChar,5),
					new SqlParameter("@Record_Date", SqlDbType.DateTime)};
			parameters[0].Value = model.Vendor_ID;
			parameters[1].Value = model.User_ID;
			parameters[2].Value = model.Vendor_Name;
			parameters[3].Value = model.Applyer;
			parameters[4].Value = model.Address;
			parameters[5].Value = model.Vendor_Image;
			parameters[6].Value = model.Vendor_Phone;
			parameters[7].Value = model.Vendor_Description;
			parameters[8].Value = model.B_Type;
			parameters[9].Value = model.longitude;
			parameters[10].Value = model.Latitude;
			parameters[11].Value = model.Delivery_MinimalMoney;
			parameters[12].Value = model.Delivery_AdditionalMoney;
			parameters[13].Value = model.Delivery_AlwaysNeedMoney;
			parameters[14].Value = model.Delivery_Description;
			parameters[15].Value = model.Delivery_Area;
			parameters[16].Value = model.Vendor_StartTime;
			parameters[17].Value = model.Vendor_EndTime;
			parameters[18].Value = model.Record_Date;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(BMSQ.Model.Vendor_List model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Vendor_List set ");
			strSql.Append("User_ID=@User_ID,");
			strSql.Append("Vendor_Name=@Vendor_Name,");
			strSql.Append("Applyer=@Applyer,");
			strSql.Append("Address=@Address,");
			strSql.Append("Vendor_Image=@Vendor_Image,");
			strSql.Append("Vendor_Phone=@Vendor_Phone,");
			strSql.Append("Vendor_Description=@Vendor_Description,");
			strSql.Append("B_Type=@B_Type,");
			strSql.Append("longitude=@longitude,");
			strSql.Append("Latitude=@Latitude,");
			strSql.Append("Delivery_MinimalMoney=@Delivery_MinimalMoney,");
			strSql.Append("Delivery_AdditionalMoney=@Delivery_AdditionalMoney,");
			strSql.Append("Delivery_AlwaysNeedMoney=@Delivery_AlwaysNeedMoney,");
			strSql.Append("Delivery_Description=@Delivery_Description,");
			strSql.Append("Delivery_Area=@Delivery_Area,");
			strSql.Append("Vendor_StartTime=@Vendor_StartTime,");
			strSql.Append("Vendor_EndTime=@Vendor_EndTime,");
			strSql.Append("Record_Date=@Record_Date");
			strSql.Append(" where Vendor_ID=@Vendor_ID ");
			SqlParameter[] parameters = {
					new SqlParameter("@User_ID", SqlDbType.NVarChar,50),
					new SqlParameter("@Vendor_Name", SqlDbType.NVarChar,50),
					new SqlParameter("@Applyer", SqlDbType.NVarChar,50),
					new SqlParameter("@Address", SqlDbType.NVarChar,200),
					new SqlParameter("@Vendor_Image", SqlDbType.NVarChar,200),
					new SqlParameter("@Vendor_Phone", SqlDbType.NVarChar,50),
					new SqlParameter("@Vendor_Description", SqlDbType.Text),
					new SqlParameter("@B_Type", SqlDbType.NVarChar,50),
					new SqlParameter("@longitude", SqlDbType.Decimal,9),
					new SqlParameter("@Latitude", SqlDbType.Decimal,9),
					new SqlParameter("@Delivery_MinimalMoney", SqlDbType.Int,4),
					new SqlParameter("@Delivery_AdditionalMoney", SqlDbType.Int,4),
					new SqlParameter("@Delivery_AlwaysNeedMoney", SqlDbType.Int,4),
					new SqlParameter("@Delivery_Description", SqlDbType.NVarChar,500),
					new SqlParameter("@Delivery_Area", SqlDbType.NVarChar,500),
					new SqlParameter("@Vendor_StartTime", SqlDbType.NVarChar,5),
					new SqlParameter("@Vendor_EndTime", SqlDbType.NVarChar,5),
					new SqlParameter("@Record_Date", SqlDbType.DateTime),
					new SqlParameter("@Vendor_ID", SqlDbType.NVarChar,50)};
			parameters[0].Value = model.User_ID;
			parameters[1].Value = model.Vendor_Name;
			parameters[2].Value = model.Applyer;
			parameters[3].Value = model.Address;
			parameters[4].Value = model.Vendor_Image;
			parameters[5].Value = model.Vendor_Phone;
			parameters[6].Value = model.Vendor_Description;
			parameters[7].Value = model.B_Type;
			parameters[8].Value = model.longitude;
			parameters[9].Value = model.Latitude;
			parameters[10].Value = model.Delivery_MinimalMoney;
			parameters[11].Value = model.Delivery_AdditionalMoney;
			parameters[12].Value = model.Delivery_AlwaysNeedMoney;
			parameters[13].Value = model.Delivery_Description;
			parameters[14].Value = model.Delivery_Area;
			parameters[15].Value = model.Vendor_StartTime;
			parameters[16].Value = model.Vendor_EndTime;
			parameters[17].Value = model.Record_Date;
			parameters[18].Value = model.Vendor_ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string Vendor_ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Vendor_List ");
			strSql.Append(" where Vendor_ID=@Vendor_ID ");
			SqlParameter[] parameters = {
					new SqlParameter("@Vendor_ID", SqlDbType.NVarChar,50)			};
			parameters[0].Value = Vendor_ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string Vendor_IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Vendor_List ");
			strSql.Append(" where Vendor_ID in ("+Vendor_IDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public BMSQ.Model.Vendor_List GetModel(string Vendor_ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 Vendor_ID,User_ID,Vendor_Name,Applyer,Address,Vendor_Image,Vendor_Phone,Vendor_Description,B_Type,longitude,Latitude,Delivery_MinimalMoney,Delivery_AdditionalMoney,Delivery_AlwaysNeedMoney,Delivery_Description,Delivery_Area,Vendor_StartTime,Vendor_EndTime,Record_Date from Vendor_List ");
			strSql.Append(" where Vendor_ID=@Vendor_ID ");
			SqlParameter[] parameters = {
					new SqlParameter("@Vendor_ID", SqlDbType.NVarChar,50)			};
			parameters[0].Value = Vendor_ID;

			BMSQ.Model.Vendor_List model=new BMSQ.Model.Vendor_List();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public BMSQ.Model.Vendor_List DataRowToModel(DataRow row)
		{
			BMSQ.Model.Vendor_List model=new BMSQ.Model.Vendor_List();
			if (row != null)
			{
				if(row["Vendor_ID"]!=null)
				{
					model.Vendor_ID=row["Vendor_ID"].ToString();
				}
				if(row["User_ID"]!=null)
				{
					model.User_ID=row["User_ID"].ToString();
				}
				if(row["Vendor_Name"]!=null)
				{
					model.Vendor_Name=row["Vendor_Name"].ToString();
				}
				if(row["Applyer"]!=null)
				{
					model.Applyer=row["Applyer"].ToString();
				}
				if(row["Address"]!=null)
				{
					model.Address=row["Address"].ToString();
				}
				if(row["Vendor_Image"]!=null)
				{
					model.Vendor_Image=row["Vendor_Image"].ToString();
				}
				if(row["Vendor_Phone"]!=null)
				{
					model.Vendor_Phone=row["Vendor_Phone"].ToString();
				}
				if(row["Vendor_Description"]!=null)
				{
					model.Vendor_Description=row["Vendor_Description"].ToString();
				}
				if(row["B_Type"]!=null)
				{
					model.B_Type=row["B_Type"].ToString();
				}
				if(row["longitude"]!=null && row["longitude"].ToString()!="")
				{
					model.longitude=decimal.Parse(row["longitude"].ToString());
				}
				if(row["Latitude"]!=null && row["Latitude"].ToString()!="")
				{
					model.Latitude=decimal.Parse(row["Latitude"].ToString());
				}
				if(row["Delivery_MinimalMoney"]!=null && row["Delivery_MinimalMoney"].ToString()!="")
				{
					model.Delivery_MinimalMoney=int.Parse(row["Delivery_MinimalMoney"].ToString());
				}
				if(row["Delivery_AdditionalMoney"]!=null && row["Delivery_AdditionalMoney"].ToString()!="")
				{
					model.Delivery_AdditionalMoney=int.Parse(row["Delivery_AdditionalMoney"].ToString());
				}
				if(row["Delivery_AlwaysNeedMoney"]!=null && row["Delivery_AlwaysNeedMoney"].ToString()!="")
				{
					model.Delivery_AlwaysNeedMoney=int.Parse(row["Delivery_AlwaysNeedMoney"].ToString());
				}
				if(row["Delivery_Description"]!=null)
				{
					model.Delivery_Description=row["Delivery_Description"].ToString();
				}
				if(row["Delivery_Area"]!=null)
				{
					model.Delivery_Area=row["Delivery_Area"].ToString();
				}
				if(row["Vendor_StartTime"]!=null)
				{
					model.Vendor_StartTime=row["Vendor_StartTime"].ToString();
				}
				if(row["Vendor_EndTime"]!=null)
				{
					model.Vendor_EndTime=row["Vendor_EndTime"].ToString();
				}
				if(row["Record_Date"]!=null && row["Record_Date"].ToString()!="")
				{
					model.Record_Date=DateTime.Parse(row["Record_Date"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select Vendor_ID,User_ID,Vendor_Name,Applyer,Address,Vendor_Image,Vendor_Phone,Vendor_Description,B_Type,longitude,Latitude,Delivery_MinimalMoney,Delivery_AdditionalMoney,Delivery_AlwaysNeedMoney,Delivery_Description,Delivery_Area,Vendor_StartTime,Vendor_EndTime,Record_Date ");
			strSql.Append(" FROM Vendor_List ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" Vendor_ID,User_ID,Vendor_Name,Applyer,Address,Vendor_Image,Vendor_Phone,Vendor_Description,B_Type,longitude,Latitude,Delivery_MinimalMoney,Delivery_AdditionalMoney,Delivery_AlwaysNeedMoney,Delivery_Description,Delivery_Area,Vendor_StartTime,Vendor_EndTime,Record_Date ");
			strSql.Append(" FROM Vendor_List ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Vendor_List ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.Vendor_ID desc");
			}
			strSql.Append(")AS Row, T.*  from Vendor_List T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Vendor_List";
			parameters[1].Value = "Vendor_ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

